package com.dr.controller;

import com.dr.domain.ConstantType;
import com.dr.domain.PageResult;
import com.dr.domain.Result;
import com.dr.service.ConstantTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: yangjianfeng
 * @Date: 2025/07/03 10:25
 * @Description:
 */
@RestController
@RequestMapping("/constanttype")
@Api("常数类型管理")
public class ConstantTypeController {
    @Autowired
    private ConstantTypeService constantTypeService;

    //查询所有常数类型数据
    @GetMapping("/getAll")
    @ApiOperation("获取所有常数类型数据")
    public Result getAllConstantType(){
        List<ConstantType> typeList = constantTypeService.list();
        return Result.success(typeList);
    }
    //分页展示常数类别数据
    @GetMapping("/page")
    @ApiOperation("分页获取常数分类")
    public Result page(@ApiParam("当前页") @RequestParam(required = true,defaultValue = "1") Integer pageNum,
                       @ApiParam("每页显示的记录条数")  @RequestParam(required = true,defaultValue = "10") Integer pageSize,
                       @ApiParam("查询条件")  @RequestParam(required = false) String condition){
        PageResult<ConstantType> pageResult= constantTypeService.page(pageNum,pageSize,condition);
        return Result.success(pageResult);
    }

    @PostMapping("/add")
    @ApiOperation("新增常数类型数据")
    public Result add(@RequestBody ConstantType constantType){

        try {
            constantTypeService.add(constantType);
            return  Result.success("success");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("新增类别失败");
        }
    }

    @PutMapping("/update")
    @ApiOperation("编辑常数类别数据")
    public Result update(@RequestBody ConstantType constantType){

        try {
            constantTypeService.updateById(constantType);
            return  Result.success("success");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("新增类别失败");
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除常数类别数据")
     //spring事务失效的场景？？？？？？
    public Result delete(Integer id){
        try {
            constantTypeService.removeById(id);
            return  Result.success("success");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("新增类别失败");
        }
    }


}
